<template>
    <div class="deptbgc">
        <el-header>
            <div class="depttitle">
                部门管理
            </div>
        </el-header>    
        <div class="btncl">
            <el-button class="deptbtn1" type="primary" @click="addDialogVisible = true">新建部门</el-button>
            <el-button class="deptbtn" type="primary" @click="deleteDialogVisible = true">删除部门</el-button>
            <el-button class="deptbtn" type="primary" @click="editDialogVisible = true">修改部门</el-button>
            <button class="deptbtn">查询部门下员工</button>
        </div>

        <el-form ref="form" :model="form">
        <div class="tableborder">
        <el-table :data="DepartmentList" class="tablestyle" :header-cell-style="{background:'#000',color:'gold'}" :row-style="{background:'rgba(0,0,0,0.1)',color:'gold'}">
                <el-table-column prop="dpnum" label="编号" ></el-table-column>
                <el-table-column prop="dpna" label="名称" ></el-table-column>
                <el-table-column prop="dptp" label="类型" ></el-table-column>
                <el-table-column prop="dptel" label="电话" ></el-table-column>
                <el-table-column prop="dped" label="成立日期" ></el-table-column>
        </el-table>
        </div>

            <el-dialog title="新建部门" :visible.sync="addDialogVisible" :append-to-body="true" @close="addDeptClosed" >
                <el-form :model="query" ref="addDeptRef">
                    <el-form-item label="部门编号" :label-width="formLabelWidth" prop="dpnum">
                        <el-input v-model="query.dpnum" ></el-input>
                    </el-form-item>
                    <el-form-item label="部门名称" :label-width="formLabelWidth" prop="dpna">
                        <el-input v-model="query.dpna" ></el-input>
                    </el-form-item>
                    <el-form-item label="部门类型" :label-width="formLabelWidth" prop="dpty">
                        <el-input v-model="query.dptp" ></el-input>
                    </el-form-item>
                    <el-form-item label="部门电话" :label-width="formLabelWidth" prop="dptel">
                        <el-input v-model="query.dptel" ></el-input>
                    </el-form-item>
                    <el-form-item label="部门成立日期" :label-width="formLabelWidth" prop="dpdate">
                        <el-input v-model="query.dped" ></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="addDialogVisible=false">取 消</el-button>
                    <el-button type="primary" @click="addDept">确 定</el-button>
                </span>
            </el-dialog>

            <el-dialog title="删除部门" :visible.sync="deleteDialogVisible" :append-to-body="true" @close="deleteDeptClosed">
                <el-form ref="deleteDeptRef">
                <el-form-item label="请输入要删除的部门编号" :label-width="formLabelWidth" prop="dpnum">
                        <el-input v-model="dpnum" ></el-input>
                </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="deleteDialogVisible=false">取 消</el-button>
                    <el-button type="primary" @click="deleteDept">确 定</el-button>
                </span>
            </el-dialog>

            <el-dialog title="修改部门" :visible.sync="editDialogVisible" :append-to-body="true" @close="editDeptClosed">
                <el-form :model="query" ref="editDeptRef">
                    <el-form-item label="部门编号" :label-width="formLabelWidth">
                        <el-input v-model="query.dpnum"></el-input>
                    </el-form-item>
                    <el-form-item label="部门名称" :label-width="formLabelWidth">
                        <el-input v-model="query.dpna"></el-input>
                    </el-form-item>
                    <el-form-item label="部门类型" :label-width="formLabelWidth">
                        <el-input v-model="query.dptp"></el-input>
                    </el-form-item>
                    <el-form-item label="部门电话" :label-width="formLabelWidth">
                        <el-input v-model="query.dptel"></el-input>
                    </el-form-item>
                    <el-form-item label="部门成立日期" :label-width="formLabelWidth">
                        <el-input v-model="query.dped"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="editDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editDept">确 定</el-button>
                </div>
            </el-dialog>
        </el-form>
    </div>
</template>

<script>
export default {
    created() {
        this.getDepartmentList();
    },
    data() {
      return {
        form: {},
        query: {
          dpnum: '',
          dpna: '',
          dptp: '',
          dptel: '',
          dped: ''
        },
        dpnum:"",
        DepartmentList: [],
        formLabelWidth: '120px',
        dialogTableVisible: false,
        addDialogVisible: false,
        deleteDialogVisible: false,
        editDialogVisible: false
      }
    },

    methods:{
        async getDepartmentList(){
            this.$axios.post("http://localhost:8081/getDepartment", this.query).then(res => {
                console.log(res.data);
                this.DepartmentList = res.data;
            });
        },
        addDeptClosed(){
            this.$refs.addDeptRef.resetFields();
        },
        deleteDeptClosed(){
            this.$refs.deleteDeptRef.resetFields();
        },
        editDeptClosed(){
            this.$refs.editDeptRef.resetFields();
        },
        async addDept() {
            this.$refs.addDeptRef.validate(async valid => {
                if (!valid) return;
                this.$axios.post("http://localhost:8081/addDepartment", this.query).then(res => {                        
                    if (res != "success") {
                    this.addDialogVisible = false;
                    this.getDepartmentList();
                    return this.$message.success("操作成功");
                    }
                        this.$message.error("操作失败");
                });
            });
        },
        async deleteDept(dpnum) {
            const confirmResult = await this.$confirm('是否确认删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).catch(err => err)
            if (confirmResult != 'confirm') {
                return this.$message.info("已取消删除");
            }
            this.$axios.delete("http://localhost:8081/deleteDepartment?dpnum=" + this.dpnum).then(res => {
                if(res != "success"){
                    this.getDepartmentList();
                    return this.$message.success("删除成功");
                }
                this.$message.error("删除失败");
            })
        },
        async showEditDialog(dpnum) {
            this.$axios.get("http://localhost:8081/upDepartment?dpnum=" + this.dpnum).then(res => {
            this.query = res.data;
            this.editDialogVisible = true;
            })
        },
        async editDept() {
            this.$refs.editDeptRef.validate(async valid => {
                if (!valid) return;
                this.$axios.post("http://localhost:8081/editDepartment", this.query).then(res => {
                    if (res != "success") {
                        this.addDialogVisible = false;
                        this.getDepartmentList();
                        return this.$message.success("操作成功");
                    }
                    this.$message.error("操作失败");
                })
            })
        }
    }
};
</script>

<style scoped>
.deptbgc{
    background: url("../assets/5.jpg");
    width: 100%;
    height: 100%;
    position: fixed;
    margin: -8px;
}

.depttitle{
    font-size: 60px;
    text-align: center;
    color: rgb(216, 185, 7);
    width: 100%;
    height: 80px;
}

.btncl{
    width: 100%;
    height: 132px;
}

.deptbtn1{
    color: gold;
    background-color: #000;
    margin-top: 46px;
    margin-left: 600px;
    font-size: 30px;
    float: left;
    border: 0px;
    cursor: pointer;
}

.deptbtn{
    color: gold;
    background-color: #000;
    margin-top: 50px;
    margin-left: 30px;
    font-size: 30px;
    float: left;
    border: 0px;
    cursor: pointer;
}

.tablestyle{
    background-color: rgba(0,0,0,0.1);
    margin-left: 300px;
    margin-top: 35px;
    width: 1350px;
    height: 600px;
}

.tableborder >>> .el-table__row>td{
  /* 去除表格线 */
  border: none;
}

/* .tableborder >>> .el-table th.is-leaf{
    去除上边框
    border: none;
} */

.el-table::before{
    /* 去除下边框 */
    height: 0px;
}

</style>
